package pr.vodafone.resources;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.sun.jersey.api.NotFoundException;

import pr.vodafone.dao.dto.Cliente;
import pr.vodafone.dao.dto.Linea;
import pr.vodafone.service.ServiceProvider;


@Path("/clientes/{dni}/lineas")
public class LineasResource {
	
	@Context
	UriInfo uriInfo;
	
	@Context
	Request request;
	
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public List<Linea> getLineas (@PathParam("dni") String dni){
		
		List<Linea> lineas = new ArrayList<Linea>();
		ServiceProvider sp = new ServiceProvider();
		lineas = sp.obtenerLineas(dni);
		/*Metodo para buscar las lineas*/
		return lineas;
	}
	
	@POST
	@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response newLinea (Linea linea, @PathParam("dni") String dni){
		Response res;
		boolean result;
		ServiceProvider sp = new ServiceProvider();
		result = sp.insertarLinea(dni, linea);
		if(result){
			URI uri = uriInfo.getAbsolutePathBuilder().path(linea.getDni()).build();
			res = Response.created(uri).entity(linea).build(); // Code: 201
		}
		else{
			res = Response.status(409).entity("Linea " + linea.getDni() +  " ya existe").build();
		}
		
		return res;
	}
	
	@Path("{linea}")
	public LineaResource getLinea(@PathParam("linea") String telefono, @PathParam("dni") String dni){
		return new LineaResource(dni,telefono);
	}
	
	

}
